package edu.tamu.cse.teamproject1.Database;

import java.util.Date;

import edu.tamu.cse.teamproject1.Database.Interfaces.ITupleElement;

/**
 * 
 * @author Ben Unsworth
 * This is a superclass to allow different tuple types
 *
 */

public class TupleElement implements ITupleElement {
	protected Object tupleValue;
	protected int size;
	protected String colName;
	protected EnumColumnType tupleType;
	boolean nullable;
	boolean key;
	
	/**
	 * interface method getValue returns the object tupleValue
	 */
	@Override
	public Object getValue() {
		return tupleValue;
	}

	
	/**
	 * set the value of this tupleElement
	 * @param value
	 */
	@Override
	public void setValue(Object value) {
		tupleValue = value;
	}

	/**
	 * @param value, columnName
	 * @return a new tupleElement created according the the parameters
	 */
	@Override
	public ITupleElement createTupleElement(Object value, String columnName) {
		// size, nullable, and type should already be set when using the constructor
		ITupleElement newElem = null;
		if (value instanceof Integer) {
			newElem = new TupleIntegerElement((Integer) value);
		} else if (value instanceof String) {
			newElem = new TupleVarCharElement((String) value);
		} else if (value instanceof varchar){
			newElem = new TupleVarCharElement((varchar) value);
		} else if (value instanceof Float){
			newElem = new TupleFloatElement((Float) value);
		} else if (value instanceof Date){
			newElem = new TupleDateElement((Date) value);
		} else {
			throw new IllegalArgumentException("Invalid Object");
		}
		newElem.setColumnName(columnName);
		return newElem;
	}

	/**
	 * @return the columnName of the TupleElement
	 */
	@Override
	public String getColumnName() {
		return colName;
	}

	/**
	 * @return the size of the tupleElement
	 */
	@Override
	public int getColumnSize() {
		return size;
	}

	/**
	 * @return the type of the TupleElement
	 */
	@Override
	public EnumColumnType getColumnType() {
		return tupleType;
	}

	
	/**
	 * @return whether the tupleElement can be null
	 */
	@Override
	public boolean isNullable() {
		return false;
	}

	
	/**
	 * Set the columnName corresponding to the TupleElement
	 * @param columName
	 */
	@Override
	public void setColumnName(String columnName) {
		colName = columnName;
	}

	
	/**
	 * set the size of the tupleElement
	 * @param columnSize
	 */
	@Override
	public void setColumnSize(int columnSize) {
		if(tupleType != EnumColumnType.varchar){
			return; //don't change the size only varchar needs to of variable size
		}else{
			size = columnSize;
		}
	}

	
	/**
	 * set the type of the TupleElement
	 * @param columnType
	 */
	@Override
	public void setColumnType(EnumColumnType columnType) {
		if(columnType == tupleType) return; //no change needed
		tupleValue = null; //the tupleValue is now invalid
		tupleType = columnType;
	}

	
	/**
	 * set whether the TupleElement should be nullable
	 * @param nullable
	 */
	@Override
	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}

	
	/**
	 * @return whether the TupleElement is a key
	 */
	@Override
	public boolean isKey() {
		return key;
	}

	
	/**
	 * return whether the TupleElement is a key
	 * @param keyIs
	 */
	@Override
	public void setAsKey(boolean keyIs) {
		key = keyIs;
	}
	
	/**
	 * Tries to clone this object
	 * 
	 * @return A copy of the object
	 */
	@Override
	public Object clone() {
		try {
			return super.clone();
		} catch(CloneNotSupportedException e) {
			return null;
		}
	}

	
	/**
	 *  implemented in class ColumnDefinition
	 */
	@Override
	public ITupleElement createEmptyTupleElement() {
		return null;
	}
}
